package com.yuhf.util.db;

import lombok.extern.log4j.Log4j;

import java.sql.*;
import java.util.List;

@Log4j
public class JdbcTemplate {

    public static int queryForCount(String sql,Object...parameters){
        int rowNumber=0;
        Connection connection=null;
        try{
            connection=DBConnection.getConnection();
            PreparedStatement psmt=connection.prepareStatement(sql);
            operationSQL(psmt,parameters);
            ResultSet rs=psmt.executeQuery();
            if(rs.next()) {
                rowNumber = rs.getInt(1);
            }
        } catch (SQLException ex) {
            log.error("database connection error.message is "+ex.getMessage());
        }finally {
            try {
                if(null!=connection&&connection.getAutoCommit()){
                    connection.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return rowNumber;
    }

    public static <E> List<E> queryForList(String sql, ResultSetHandler<List<E>> rsh, Object...parameters){
        List<E> e=null;
        Connection connection=null;
        try{
            connection=DBConnection.getConnection();
            PreparedStatement psmt=connection.prepareStatement(sql);
            operationSQL(psmt,parameters);
            ResultSet rs=psmt.executeQuery();
            e=rsh.handler(rs);
        } catch (SQLException ex) {
            log.error("database connection error.message is "+ex.getMessage());
        } finally {
            try {
                if(null!=connection&&connection.getAutoCommit()){
                    connection.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return e;
    }

    public static int update(String sql,Object...parameters){
        int rowNumber=0;
        Connection connection=null;
        try{
            connection=DBConnection.getConnection();
            PreparedStatement psmt=connection.prepareStatement(sql);
            operationSQL(psmt,parameters);
            rowNumber=psmt.executeUpdate();
        } catch (SQLException e) {
            log.error("udpate error.message is "+e.getMessage());
        }finally {
            try {
                if(null!=connection&&connection.getAutoCommit()){
                    connection.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return rowNumber;
    }

    public static int insertForKey(String sql,Object...parameters){
        int rowNumber=0;
        int id=0;
        Connection connection=null;
        if(null!=sql&&"insert".equalsIgnoreCase(sql.split(" ")[0])) {
            try {
                connection=DBConnection.getConnection();
                PreparedStatement psmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                operationSQL(psmt, parameters);
                rowNumber = psmt.executeUpdate();
                ResultSet rs = psmt.getGeneratedKeys();
                if (rs.next()){ id = rs.getInt(1);}
            } catch (SQLException e) {
                System.out.println("insert error.message is " + e.getMessage());
            }finally {
                try {
                    if(null!=connection&&connection.getAutoCommit()){
                        connection.close();
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }else{
            throw new IllegalArgumentException("SQL参数错误，不是一个插入语句");
        }
        return id;
    }

    private static void operationSQL(PreparedStatement psmt,Object...parameters){
        int parameterCount= 0;
        try {
            parameterCount = psmt.getParameterMetaData().getParameterCount();
            if(null!=parameters&&parameters.length!=0){
                if(parameterCount!=parameters.length){
                    throw new IllegalArgumentException("参数错误或参数个数不对应");
                }else{
                    for(int i=0;i<parameterCount;i++){
                        psmt.setObject(i+1,parameters[i]);
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
